﻿using Sidvall.IO.Security;
using Sidvall.Net;
using Sidvall.Security;

namespace Sidvall.IO
{
    public class NetSystemContext
    {
        #region Public Members

        public IAuthenticationStrategy AuthenticationStrategy { get; set; }
        public INetFileSystemStrategy NetFileSystemStrategy { get; set; }
        public string StoreId { get; set; }
        public string BaseAddress { get; set; }
        public int DefaultBufferSize { get; set; }

        #region GetWebAddress

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        internal string GetWebAddress(INetFileSystemStrategy strategy, ResourceType resource, FileSystemOperation action, HttpMethod method, string storeId)
        {
            return GetWebAddress(strategy, resource, action, method, storeId, null);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        internal string GetWebAddress(INetFileSystemStrategy strategy, ResourceType resource, FileSystemOperation action, HttpMethod method, string storeId, string path)
        {
            if (strategy != null)
                return strategy.GetHttpRequestAddress(resource, action, method, storeId, path);
            var address = "api/V1/NetFileSystem/" + storeId + "/" + resource.ToString() + "/" + action.ToString();
            if (!string.IsNullOrWhiteSpace(path))
                address += "/" + path;
            return address;
        }

        #endregion

        #endregion
        #region Factory Members

        #region Current

        private static readonly object _Mutex = new object();
        private static volatile NetSystemContext _Current;
        public static NetSystemContext Current
        {
            get
            {
                NetSystemContext item;

                if (_Current == null)
                {
                    lock (_Mutex)
                    {
                        if (_Current == null)
                        {
                            item = new NetSystemContext()
                            {
                                DefaultBufferSize = 250000,
                            };
                            _Current = item;
                        }
                    }
                }
                return _Current;
            }
            set
            {
                lock (_Mutex)
                {
                    _Current = value;
                }
            }
        }

        #endregion

        #endregion
    }
}
